//
//  NetworkTools.swift
//  Weibo项目
//
//  Created by vn on 16/7/22.
//  Copyright © 2016年 vn. All rights reserved.
//

import UIKit
import AFNetworking

enum NetworkToolsType:String{
    
    case GET = "GET"
    case POSt = "POST"
    
}


class NetworkTools: AFHTTPSessionManager {

    
    //全局访问点
    static let sharedManagerTolls: NetworkTools = {
        
        let tools = NetworkTools()
        //设置反序列化器
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
        
    }()
    
    //网络请求方法
    func request(type:NetworkToolsType,url:String,parameters:AnyObject?,success:(response:AnyObject?)->(),failure:(error:NSError)->()){
        
        
        if type == .GET{
            
            //get请求
            self .GET(url, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                
                    success(response: response)
                
               
                }, failure: { (_, error) -> Void in
                    
                    failure(error: error)
                    
            })
            
        }else{
            //post请求
             self .POST(url, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                
                success(response: response)
                
               
                }, failure: { (_, error) -> Void in
                    
                    failure(error: error)
                    
            })

        
        }
    
    
    }


    //请求access_token
    func getAccessToken(code:String,success:(response: AnyObject?)->(),failure:(error: NSError)->()){
        
        
        let urlStr = "https://api.weibo.com/oauth2/access_token"
        //请求参数
        let parameters = [
            "client_id": APPKEY,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": APPREDIRECT_URI,
            "client_secret": APPSECRET
        ]
        
        //掉用上面方法,
        request(NetworkToolsType.POSt, url: urlStr, parameters: parameters, success: success, failure: failure)
        
    }
    
    //请求用户信息
   func getUserInfo(Model: accessTokenModel,success:(response:AnyObject?)->(),failure:(error:NSError)->()){
        
        let url = "https://api.weibo.com/2/users/show.json"
        
        let params = [
            "access_token": Model.access_token!,
            "uid": Model.uid!
        ]
        
        request(NetworkToolsType.GET, url:url, parameters: params , success:success ,failure:failure)
        
    }
    
      
}



//请求微博主页信息
extension NetworkTools{
    
    //获取首页微博信息
    func getHomeDataInfo(success:(response:AnyObject?)->(),failure:(error:NSError)->()){
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        // 请求参数
        let params = ["access_token": OAuthViewModel.sharedViewModel.accessToken!]
        
        request(NetworkToolsType.GET, url:urlString, parameters:params, success: success, failure: failure)
        
        
    }
    

}









